<template>
	<view class="wx-authorization-modal" v-if="show">
		<view class="wam__mask" @touchmove.prevent="" @tap.stop="close"></view>

		<!-- 内容区域 -->
		<view class="wam__wrapper">
			<!-- 关闭按钮 -->
			<view class="wam__close-btn" @tap.stop="close">
				<text class="tn-icon-close"></text>
			</view>
			<!-- 标题 -->
			<view class="wam__title">获取您的昵称、头像</view>
			<!-- tips -->
			<view class="wam__sub-title">
				获取用户头像、昵称，主要用于向用户提供具有辨识度的用户中心界面
			</view>

			<!-- 头像选择 -->
			<view class="wam__avatar">
				<view class="button-shadow">
					<button class="button" open-type="chooseAvatar" @chooseavatar="chooseAvatarEvent">
						<view v-if="userInfo.avatar" class="avatar__image">
							<image class="image" :src="userInfo.avatar" mode="aspectFill"></image>
						</view>
						<view v-else class="avatar__empty">

						</view>
						<view class="avatar--icon">
							<view class="tn-icon-camera-fill"></view>
						</view>
					</button>
				</view>
			</view>

			<!-- 昵称输入 -->
			<view class="wam__nickname">
				<view class="nickname__data">
					<input class="input" type="nickname" v-model="userInfo.nickname" placeholder="请输入昵称"
						placeholder-style="color: #AAAAAA;">
				</view>
			</view>
			
			<view style="margin-top: 30rpx;">
				<rui-button @tap="$rui.throttle(submit, 2000)" title="登录"
					:disabled="!userInfo.avatar || !userInfo.nickname"></rui-button>
			</view>
			
			<!-- 保存按钮 -->
			<!-- 	<view class="wam__submit-btn" :class="[{
          'disabled': !userInfo.avatar || !userInfo.nickname
        }]" hover-class="tn-btn-hover-class" :hover-stay-time="150" @tap.stop="submit">
				登录
			</view> -->
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from "vue"

	import {
		saveUserLogin,
		checkUserLogin
	} from "@/common/api.js"
	import cache from "@/util/cache.js"

	let userInfo = ref({
		avatar: '',
		nickname: '',
		type: 'wechat',
	})
	let show = ref(false);
	let is_register = ref(true);

	// 头像选择
	const chooseAvatarEvent = (e) => {
		userInfo.value.avatar = e.detail.avatarUrl
	}

	const emits = defineEmits(['change'])
	const submit = () => {
		if (!userInfo.value.avatar) {
			uni.$rui.toast('请点击头像授权')
			return false;
		}
		if (!userInfo.value.nickname) {
			uni.$rui.toast('请您的输入昵称')
			return false;
		}
		//获取推广Code
		userInfo.value.promo = uni.getStorageSync("Promo") || "";
		saveUserLogin(userInfo.value).then(res => {
			handle(res.data)
		})

	}

	const handle = (data) => {
		cache.put('UserToken', data.token)
		delete data.token
		cache.put('UserInfo', data)
		show.value = false;
		uni.$rui.toast('登录成功', () => {
			emits('change', data)
		}, 1000)
	}

	const close = () => {
		show.value = false;
	}
	const login = () => {
		uni.showLoading({
			title: '加载中'
		});
		uni.login({
			provider: 'weixin',
			success: function(loginRes) {
				checkUserLogin({
					code: loginRes.code
				}).then(res => {
					uni.hideLoading();
					show.value = true;
					if (res.msg == 'error') {
						userInfo.value.openid = res.data.openid;
						is_register.value = false;
						return false;
					}
					userInfo.value = res.data;
				})

			}
		});
	}

	defineExpose({
		login
	})
</script>

<style lang="scss" scoped>
	.wx-authorization-modal {
		position: fixed;
		left: 0;
		top: 0;
		width: 100vw;
		height: 100vh;
		z-index: 99998;
		box-sizing: border-box;


		.image {
			width: 100%;
			height: 100%;
			border-radius: inherit;
		}

		.wam {

			/* mask */
			&__mask {
				position: absolute;
				left: 0;
				top: 0;
				width: 100%;
				height: 100%;
				background-color: rgba(0, 0, 0, 0.5);
				opacity: 0;
				animation: showMask 0.25s ease 0.1s forwards;
			}

			/* close-btn */
			&__close-btn {
				position: absolute;
				top: 30rpx;
				right: 30rpx;
				z-index: 99999;
			}

			/* wrapper */
			&__wrapper {
				position: absolute;
				left: 0;
				bottom: 0;
				width: calc(100% - 80rpx);
				background-color: #FFFFFF;
				border-radius: 20rpx 20rpx 0rpx 0rpx;
				padding: 40rpx;
				padding-top: 60rpx;
				padding-bottom: 40rpx;
				padding-bottom: calc(constant(safe-area-inset-bottom) + 40rpx);
				padding-bottom: calc(env(safe-area-inset-bottom) + 40rpx);
				transform-origin: center bottom;
				transform: scaleY(0);
				animation: showWrapper 0.25s ease 0.1s forwards;
				z-index: 99999;
			}

			/* title */
			&__title {
				font-size: 34rpx;
			}

			/* sub-title */
			&__sub-title {
				font-size: 26rpx;
				color: #AAAAAA;
				margin-top: 16rpx;
				padding-bottom: 30rpx;
			}

			/* 头像选择 */
			&__avatar {
				width: 100%;
				margin-top: 30rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				.button-shadow {
					border: 8rpx solid rgba(255, 255, 255, 0.05);
					box-shadow: 0rpx 0rpx 80rpx 0rpx rgba(0, 0, 0, 0.15);
					border-radius: 50%;
				}

				.button {
					position: relative;
					width: 160rpx;
					height: 160rpx;
					border-radius: 50%;
					overflow: visible;
					background-image: repeating-linear-gradient(45deg, #E4E9EC, #F8F7F8);
					color: #FFFFFF;
					background-color: transparent;
					padding: 0;
					margin: 0;
					font-size: inherit;
					line-height: inherit;
					border: none;

					&::after {
						border: none;
					}
				}

				.avatar {

					&__empty,
					&__image {
						width: 100%;
						height: 100%;
						border-radius: inherit;
					}

					&--icon {
						position: absolute;
						right: -10rpx;
						bottom: -6rpx;
						width: 60rpx;
						height: 60rpx;
						// transform: translate(50%, 50%);
						background-color: #1D2541;
						color: #FFFFFF;
						border-radius: 50%;
						border: 6rpx solid #FFFFFF;
						line-height: 1;
						font-size: 36rpx;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
			}

			/* 昵称 */
			&__nickname {
				margin-top: 40rpx;

				.nickname {

					&__data {
						margin-top: 16rpx;
						// width: 100%;
						padding: 26rpx 20rpx;
						border-radius: 10rpx;
						background-color: #F8F7F8;

						.input {
							color: #080808;
						}
					}
				}
			}

			/* 保存按钮 */
			&__submit-btn {
				// width: 100%;
				background-color: #05C160;
				color: #FFFFFF;
				margin-top: 60rpx;
				border-radius: 10rpx;
				padding: 25rpx;
				font-size: 32rpx;
				display: flex;
				align-items: center;
				justify-content: center;

				&.disabled {
					background-color: #E6E6E6;
				}
			}


		}
	}

	.tn-btn-hover-class {
		box-shadow: inset 10rpx 2rpx 40rpx 0rpx rgba(0, 0, 0, 0.05);
	}

	@keyframes showMask {
		0% {
			opacity: 0;
		}

		100% {
			opacity: 1;
		}
	}

	@keyframes showWrapper {
		0% {
			transform: scaleY(0);
		}

		100% {
			transform: scaleY(1);
		}
	}
</style>